package com.afkt.project.base.app;
import com.tencent.mmkv.*;
import ohos.aafwk.ability.AbilityPackage;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import com.dev.other.EventBusUtils;
import ohos.aafwk.ability.AbilityPackage;
import org.greenrobot.eventbus.EventBus;

public class MyApplication extends AbilityPackage implements MMKVHandler, MMKVContentChangeNotification {
    private final int MY_lOG=0x748746;
    HiLogLabel Label=new HiLogLabel(HiLog.LOG_APP,MY_lOG,"Log_Application");

    @Override
    public MMKVRecoverStrategic onMMKVCRCCheckFail(String mmapID) {
        return MMKVRecoverStrategic.OnErrorRecover;
    }

    @Override
    public MMKVRecoverStrategic onMMKVFileLengthError(String mmapID) {
        return MMKVRecoverStrategic.OnErrorRecover;
    }

    @Override
    public boolean wantLogRedirecting() {
        return true;
    }

    @Override
    public void mmkvLog(MMKVLogLevel level, String file, int line, String func, String message) {
        String log = "<" + file + ":" + line + "::" + func + "> " + message;
        switch (level) {
            case LevelDebug:
                HiLog.debug(Label,"redirect logging MMKV", log);
                break;
            case LevelNone:
            case LevelInfo:
                HiLog.info(Label,"redirect logging MMKV", log);
                break;
            case LevelWarning:
                HiLog.warn(Label,"redirect logging MMKV", log);
                break;
            case LevelError:
                HiLog.error(Label,"redirect logging MMKV", log);
                break;
        }
    }

    @Override
    public void onContentChangedByOuterProcess(String mmapID) {
        HiLog.info(Label,"MMKV", "other process has changed content of : " + mmapID);
    }
    @Override
    public void onInitialize() {
        super.onInitialize();
        EventBus.initThreadForHos(getUITaskDispatcher());
//MMKV初始化
//        String dir = getFilesDir().getAbsolutePath() + "/entry";
//        String rootDir = MMKV.initialize(dir, null, MMKVLogLevel.LevelInfo);
//        HiLog.info(Label,"MMKV", "mmkv root: " + rootDir);
//        HiLog.info(Label,"MMKV", "mmkv version: " + MMKV.version());
//        MMKV.setLogLevel(MMKVLogLevel.LevelInfo);
//        MMKV.registerHandler(this);
//        MMKV.registerContentChangeNotify(this);
    }
}
